﻿
@{
    ViewBag.Title = "RoleMenuRelaEdit";
    Layout = "~/Views/Shared/_Layout.cshtml";
}
@section style{
    <link href="@Url.Content("~/css/plat/blue.css")" rel="stylesheet">
    <style type="text/css">
        .panel-mainhead {
            border-bottom: 1px solid #d2d6de;
            line-height: 30px;
            font-weight: bold;
            margin-top: 10px;
        }

            .panel-mainhead input {
                margin-top: 9px;
            }

            .panel-mainhead label {
                margin-left: 9px;
                padding-top: 5px;
            }
        .chk-inline {
            float:left; 
            width:130px; 
            margin-bottom:10px;
        }
    </style>
}
<form id="form1">
    <section class="content">
        <div class="row">
            <div class="col-md-12">
                <div class="box-body" id="tbodylist">
                </div>
                <div class="box-footer text-center">
                    <button class="btn btn-primary" type="button" id="btnSave">确 定 </button>
                    <button class="btn" type="button" id="cancel">取 消 </button>
                </div>
            </div>
        </div>
    </section>
</form>
@section scriptsBottom{
    <script src="@Url.Content("~/js/icheck.min.js")"></script>
    <script src="@Url.Content("~/lib/art-template/template-web.js")"></script>
    <script id="dataItemTpl" type="text/html">
        {{each data}}
        <p class="panel-mainhead">
            {{if $value.RelaId>0}}
            <input type="checkbox" name="chkMenu" class="ckb" value="{{$value.MenuID}}" checked="checked">
            {{else}}
            <input type="checkbox" name="chkMenu" class="ckb" value="{{$value.MenuID}}">
            {{/if}}
            <label>{{$value.MenuName}}</label>
        </p>
        <div class="panel-body">
            {{include 'childTemplate' $value}} <!--引入子模板-->
        </div>
        {{/each}}
    </script>
    <script id="childTemplate" type="text/html">
        {{each ChildMenu}}
        <p class="chk-inline">
            {{if $value.RelaId>0}}
            <input type="checkbox" name="chkMenu" value="{{$value.MenuID}}" checked="checked">
            {{else}}
            <input type="checkbox" name="chkMenu" value="{{$value.MenuID}}">
            {{/if}}
            {{$value.MenuName}}
        </p>
        {{/each}}
    </script>
    <script type="text/javascript">
        $(document).ready(function () {
            $("#cancel").click(function () {
                BMCW.closeTopLayer();
            });
            $("#btnSave").click(function () {
                save();
            });

            initData();

        });

        //查询
        function initData() {
            var url = '@Url.Action("GetRoleMenuRelaList", "Authority")?roleId=@ViewBag.RoleId';
            //查询列表
            $.ajax({
                url: url,
                type: 'GET',
                dataType: "json",
                success: function (data) {
                    if (data.data.length < 1) {
                        $("#nodata").show();
                        document.getElementById('tbodylist').innerHTML = "";
                        return;
                    }
                    $("#nodata").hide();
                    //模板渲染
                    var html = template('dataItemTpl', data);
                    document.getElementById('tbodylist').innerHTML = html;
                    $('input').iCheck({
                        checkboxClass: 'icheckbox_flat-blue'
                    });
                    checkEvent();
                }
            });
        }

        //全选事件绑定
        function checkEvent() {
            $("#tbodylist").find(".panel-mainhead input:checkbox").each(function () {
                $(this).on('ifChanged', function () {
                    var $thisCheck = false;
                    if ($(this).is(':checked')) {
                        $thisCheck = true;
                    }
                    $(this).parent().parent().next("div").find(".chk-inline input:checkbox").each(function () {
                        if ($thisCheck) {
                            $(this).iCheck('check');
                        } else {
                            $(this).iCheck('uncheck');
                        }
                    });
                });

            });
            $("#tbodylist").find(".panel-body").each(function () {
                var $this = $(this);
                var checkboxes = $this.find("[type='checkbox']");
                $this.find("[type='checkbox']").each(function () {
                    $(this).on('ifChanged', function () {
                        var obj = $this.prev("p").find("input:checkbox");
                        if (checkboxes.filter(':checked').length == 0) {
                            obj.prop("checked", false);
                        } else {
                            obj.prop('checked', 'checked');
                        }
                        obj.iCheck('update');
                    });
                })

            });

        }

        //保存
        function save() {
            var url = '@Url.Action("SaveRoleMenu", "Authority")';
            var arrData = $("#form1").serializeArray();
            var data = {};
            var ChkMenuIds = "";
            for (var i = 0; i < arrData.length; i++) {
                if (arrData[i].name=="chkMenu") {
                    if (ChkMenuIds == "") {
                        ChkMenuIds += arrData[i].value;
                    } else {
                        ChkMenuIds += "," + arrData[i].value;
                    }
                }

            }

            BMCW.ajax({
                url: url,
                type: "POST",
                data: {
                    RoleID:@ViewBag.RoleId,
                    ChkMenuIds:ChkMenuIds
                },
                success: function (o) {
                    if (o.status == 0) {
                        BMCW.msg("保存成功", function () {
                            BMCW.getTopSelectedWin().initData();
                        });
                    } else {
                        BMCW.alert("保存失败");
                    }
                }
            });
        }

    </script>
}




